﻿using MJTop.Model.SYS;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MJTop.DAL.SYS
{
    public class MenuDAL : MJTop.DAL.DAO.DAL
    {
        public List<Tree> GetAllMenu()
        {
            return DbTop.GetList<Tree>("SYS_Menu");
        }


        public Retful Upsert(NameValueCollection dict, SYS_USER user)
        {
            string id = dict.GetValue(string.Empty, "id");
            string pid = dict.GetValue(string.Empty, "pid");
            int level = dict.GetValue(-1, "level");
            if (level >= 5)
            {
                return Retful.CreateFail("该级别下不允许添加子菜单！");
            }
            string name = dict.GetValue(string.Empty, "name");
            string url = dict.GetValue(string.Empty, "url");

            if (level == 0)
            {
                return Retful.CreateFail("顶级的菜单项不能进行修改！");
            }

            if (string.IsNullOrWhiteSpace(pid) && level != 0)
            {
                return Retful.CreateFail("菜单项的父ID不能为空！");
            }

            // url 需要 去除前后两端 空白，防止请求 生成的url 报错！
            dict["url"] = url.Trim();

            bool isUpdate = true;
            if (string.IsNullOrWhiteSpace(id))
            {
                isUpdate = false;
            }

            string cnt_sql = "Select Count(1) From SYS_MENU Where level=@level And name = @name ";
            int cnt = DbTop.Single(cnt_sql, -1, new { level = level, name = name });
            //当前时新增节点， 查询 name 是否在数据库中是否存在
            if (!isUpdate && cnt > 0)
            {
                return Retful.CreateFail("当前同级节点下已存在！[" + name + "]");
            }

            //如果 是 新增，则 无Id,需要查询得出 指定Id
            if (!isUpdate)
            {
                dict["id"] = NextTreeCode("M", dict["level"], "SYS_MENU");
            }

            //检查 新增或编辑 节点时  检查当前 节点的 父节点 是否在 数据库中存在！！
            string cnt_sql2 = "Select Count(1) From SYS_MENU Where id = @id ";

            int cnt2 = DbTop.Single(cnt_sql2, -1, new { id = pid });

            if (cnt2 <= 0)
            {
                return Retful.CreateFail("未找到父节点！[" + dict["pid"] + "]");
            }


            if (!isUpdate)
            {
                dict["Added"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                dict["AddedUid"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
            }
            dict["lastModified"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
            dict["lastModifiedUid"] = user.ID.ToString();

            try
            {
                if (isUpdate)
                {
                    DbTop.Update(dict, "SYS_MENU");
                    return Retful.CreateSuccess(null, "更新成功！");
                }
                else
                {
                    DbTop.Insert(dict, "SYS_MENU");
                    return Retful.CreateSuccess(null, "添加成功！");
                }
            }
            catch (Exception ex)
            {
                return Retful.CreateFail(ex.Message);
            }
        }

        public int DelMenu(string[] ids)
        {
            return DbTop.Delete("SYS_Menu", "Id", ids);
        }
    }
}
